package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.Setmeal;
import com.health.service.SetMealService;
import com.health.utils.QiNiuUtil;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    /**
     * 编辑套餐
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkGroupIds) {
        int count = setMealService.update(setmeal, checkGroupIds);
        if (count > 0) {
            return new Result(true, "编辑套餐成功");
        }

        return new Result(false, "编辑套餐失败");
    }

    /**
     * 根据套餐ID查询关联到的检查组ID
     */
    @GetMapping("/findCheckGroupIds/{id}")
    public Result findCheckGroupIds(@PathVariable Integer id) {
        List<Integer> checkGroupIds = setMealService.findCheckGroupIds(id);

        return new Result(true, "查询关联的检查组成功", checkGroupIds);
    }

    /**
     * 根据ID查询套餐
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        Setmeal setmeal = setMealService.findById(id);

        //封装套餐与图片的域名
        Map<String, Object> map = new HashMap<>();
        map.put("setmeal", setmeal);
        map.put("domain", QiNiuUtil.DOMAIN);

        return new Result(true, "查询套餐成功", map);
    }

    /**
     * 新增套餐
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkGroupIds) {
        int count = setMealService.add(setmeal, checkGroupIds);
        if (count > 0) {
            return new Result(true, "新增套餐成功");
        }
        return new Result(false, "新增套餐失败");
    }

    /**
     * 上传套餐图片
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {
        //使用七牛云保存图片
        String imgName = QiNiuUtil.uploadFile(imgFile.getInputStream());
        //封装文件名与域名
        Map<String, Object> map = new HashMap<>();
        map.put("imgName", imgName);
        map.put("domain", QiNiuUtil.DOMAIN);

        return new Result(true, "套餐图片上传成功", map);
    }

    /**
     * 删除套餐
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')") //拥有ROLE_ADMIN角色才能访问
    public Result deleteById(@PathVariable Integer id) {
        int count = setMealService.deleteById(id);
        if (count > 0) {
            return new Result(true, "删除套餐成功");
        }

        return new Result(false, "删除套餐失败");
    }

    /**
     * 套餐分页条件查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    //@PreAuthorize("hasAuthority('ROLE_QUERY')") //拥有ROLE_QUERY权限才能访问
    public Result findPage(@PathVariable Integer pageNow, @PathVariable Integer pageSize, String query) {
        PageResult<Setmeal> pageResult = setMealService.findPage(pageNow, pageSize, query);

        //封装返回结果
        return new Result(true, "套餐分页查询成功", pageResult);
    }

    /**
     * 查询套餐详情
     */
    @GetMapping("/findDetail/{id}")
    public Result findDetail(@PathVariable Integer id) {
        Setmeal setmeal = setMealService.findDetail(id);

        //封装套餐列表与图片所在域名
        Map<String, Object> map = new HashMap<>();
        map.put("setmeal", setmeal);
        map.put("domain", QiNiuUtil.DOMAIN);

        return new Result(true, "查询套餐详情成功", map);
    }
    /**
     * 查询所有套餐
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Setmeal> setmealList = setMealService.findAll();

        //封装套餐列表与图片所在域名
        Map<String, Object> map = new HashMap<>();
        map.put("list", setmealList);
        map.put("domain", QiNiuUtil.DOMAIN);

        return new Result(true, "查询所有套餐成功", map);
    }

    /**
     * 根据条件查询套餐
     */
    @GetMapping("/findByQuery")
    public Result findByQuery(String query){

        List<Setmeal> setmeals = setMealService.findByQuery(query);
        return new Result(true,"条件查询套餐成功",setmeals);
    }
}
